﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcControlToolkit
{
    /// <summary>
    /// The CollapsiblePanel is a very flexible extender that allows you to easily 
    /// add collapsible sections to your web page. This extender targets any ASP.NET
    /// Panel control. The page developer specifies which control(s) on the page 
    /// should be the open/close controller for the panel, or the panel can be set 
    /// to automatically expand and/or collapse when the mouse cursor moves in or 
    /// out of it, respectively
    /// </summary>
    public static class CollapsiblePanelExtensions
    {
        private static readonly CollapsiblePanelExpandDirection DEFAULT_EXPAND = CollapsiblePanelExpandDirection.Vertical;

        public static string CollapsiblePanel(this AjaxHelper helper, string elementId, bool collapsed, string expandControlId, string collapseControlId)
        {
            return helper.CollapsiblePanel(elementId, 0, 0, collapsed, expandControlId, collapseControlId);
        }

        public static string CollapsiblePanel(this AjaxHelper helper, string elementId, int collapsedSize, int expandedSize, bool collapsed, string expandControlId, string collapseControlId)
        {
            return helper.CollapsiblePanel(elementId, collapsedSize, expandedSize, collapsed, expandControlId, collapseControlId, DEFAULT_EXPAND);
        }

        public static string CollapsiblePanel(this AjaxHelper helper, string elementId, int collapsedSize, int expandedSize, bool collapsed, string expandControlId, string collapseControlId, CollapsiblePanelExpandDirection expandDirection)
        {
            return helper.CollapsiblePanel(elementId, collapsedSize, expandedSize, collapsed, expandControlId, collapseControlId, false, expandDirection);
        }

        public static string CollapsiblePanel(this AjaxHelper helper, string elementId, int collapsedSize, int expandedSize, bool collapsed, string expandControlId, string collapseControlId, bool scrollContents, CollapsiblePanelExpandDirection expandDirection)
        {
            return helper.CollapsiblePanel(elementId, collapsedSize, expandedSize, collapsed, expandControlId, collapseControlId, false, false, scrollContents, expandDirection);
        }

        public static string CollapsiblePanel(this AjaxHelper helper, string elementId, int collapsedSize, int expandedSize, bool collapsed, string expandControlId, string collapseControlId, bool autoCollapse, bool autoExpand, bool scrollContents, CollapsiblePanelExpandDirection expandDirection)
        {
            return helper.CollapsiblePanel(elementId, collapsedSize, expandedSize, collapsed, expandControlId, collapseControlId, autoCollapse, autoExpand, scrollContents, "", "", "", "", "", "", expandDirection);
        }

        public static string CollapsiblePanel(this AjaxHelper helper, string elementId, int collapsedSize, int expandedSize, bool collapsed, string expandControlId, string collapseControlId, bool autoCollapse, bool autoExpand, bool scrollContents, string textLabelId, string collapsedText, string expandedText, string imageControlId, string expandedImage, string collapsedImage, CollapsiblePanelExpandDirection expandDirection)
        {
            RouteValueDictionary props = new RouteValueDictionary();
            props.Add("ClientStateFieldID", string.Format("{0}_{1}", elementId, "collapsedState"));
            props.Add("SuppressPostBack", true);

            props.AddIfNotDefault("CollapsedSize", collapsedSize);
            props.AddIfNotDefault("ExpandedSize", expandedSize);
            props.AddIfNotEqual("Collapsed", collapsed, false);
            props.AddIfNotDefault("ExpandControlID", expandControlId);
            props.AddIfNotDefault("CollapseControlID", collapseControlId);            
            props.AddIfNotEqual("AutoCollapse", autoCollapse, false);
            props.AddIfNotEqual("AutoExpand", autoExpand, false);
            props.AddIfNotEqual("ScrollContents", scrollContents, false);
            props.AddIfNotEqual("ExpandDirection", expandDirection, DEFAULT_EXPAND);
            props.AddIfNotDefault("TextLabelID", textLabelId);
            props.AddIfNotDefault("ExpandedText", expandedText);
            props.AddIfNotDefault("CollapsedText", collapsedText);
            props.AddIfNotDefault("ImageControlID", imageControlId);
            props.AddIfNotDefault("ExpandedImage", expandedImage);
            props.AddIfNotDefault("CollapsedImage", collapsedImage);

            return helper.CollapsiblePanel(elementId, props);
        }

        /// <summary>
        /// Creates a CollapsiblePanel extender with the specified properties
        /// </summary>
        /// <param name="elementId">ID of the Panel (div) to operate expand and collapse</param>
        /// <param name="properties">An object containing the properties for this extender</param>
        public static string CollapsiblePanel(this AjaxHelper helper, string elementId, object properties)
        {
            var sb = new StringBuilder();

            // Add Microsoft Ajax library   
            sb.Append(helper.MicrosoftAjaxInclude());

            // Add toolkit scripts   
            sb.Append(helper.ToolkitInclude(
                    "AjaxControlToolkit.ExtenderBase.BaseScripts.js",
                    "AjaxControlToolkit.Common.Common.js",
                    "AjaxControlToolkit.Animation.Animations.js",
                    "AjaxControlToolkit.Compat.Timer.Timer.js",
                    "AjaxControlToolkit.CollapsiblePanel.CollapsiblePanelBehavior.js"
                ));

            //add state field
            sb.AppendFormat("<input type=\"hidden\" name=\"{0}\" id=\"{0}\" />", string.Format("{0}_{1}", elementId, "collapsedState"));

            // Perform $create               
            sb.AppendLine(helper.Create("AjaxControlToolkit.CollapsiblePanelBehavior", properties, elementId));

            return sb.ToString();
        }
    }

    #region [Enumerations]

    public enum CollapsiblePanelExpandDirection
    {
        Horizontal = 0,
        Vertical = 1,
    }

    #endregion
}
